﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.Http;

using src;
using src.Models.Requests;
using src.Models.Responses;
using Newtonsoft.Json;
using System.Diagnostics;

namespace ClientTest
{
    public class Program
    {
        static void Main(string[] args)
        {
            Test client = new Test();
            client.startClient().GetAwaiter().GetResult();
        }

        
    }

    public class Test
    {
        public static String baseAddress = "http://localhost:3000/";
        ApiClient apiClient;
        public static String Token;

        Stopwatch timePerParse;

        public Test()
        {
            apiClient = new ApiClient(baseAddress);
        }

        public async Task startClient()
        {
            //var user = new UserRequest() { Name = "Vasile", Email = "email@email.ro", Password = "password", PasswordConfirmation = "password" };
            //var response = await apiClient.SignUp(user);

            timePerParse = Stopwatch.StartNew();

            /* authentication request */
            var auth = new AuthenticateRequest() { Email = "email@email.ro", Password = "password" };
            var response = await apiClient.SignIn(auth);

            timePerParse.Stop();
            Console.WriteLine("Time for authenticating: {0}", timePerParse.ElapsedMilliseconds);
            
            MessageResponse msgResp = response as MessageResponse;
            Console.WriteLine(msgResp.Message);
            if (Convert.ToInt32(msgResp.StatusCode) == 201)
            {
                ApiClient.Token = msgResp.Content;
            }
            else
            {
                return;
            }

            timePerParse = Stopwatch.StartNew();
            /* get a project with a certain id */
            response = await apiClient.GetProject(3);

            timePerParse.Stop();
            Console.WriteLine("Time for getting a project: {0}", timePerParse.ElapsedMilliseconds);

            timePerParse = Stopwatch.StartNew();
            /* post new project request */
            var projectReq = new ProjectRequest() { Name = "Another Project" };
            response = await apiClient.PostProject(projectReq);

            timePerParse.Stop();
            Console.WriteLine("Time for posting a project: {0}", timePerParse.ElapsedMilliseconds);

            if (response is ProjectResponse)
            {
                timePerParse = Stopwatch.StartNew();
                /* delete the project that I've just posted */
                int id = (response as ProjectResponse).Id;
                response = await apiClient.DeleteProject(id);
                Console.WriteLine((response as MessageResponse).Message);
                timePerParse.Stop();
                Console.WriteLine("Time for destroying a project: {0}", timePerParse.ElapsedMilliseconds);
            }
            else if (response is MessageResponse)
                Console.WriteLine("message");
            else
                Console.WriteLine("neither");


            ///* get all the projects */
            //var projects = await apiClient.ListProjects();
            //foreach (ProjectResponse project in projects)
            //{
            //    Console.WriteLine("project name: " + project.Name);
            //}
        }
    }
}
